package com.dly.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dly.blog.domain.FreeFrequency;
import com.dly.blog.domain.Product;
import com.dly.blog.domain.vo.ProductVO;
import com.dly.blog.dto.email.FileDTO;
import com.dly.blog.mapper.FreeFrequencyMapper;
import com.dly.blog.mapper.ProductMapper;
import com.dly.blog.provider.file.FileServiceProvider;
import com.dly.blog.service.ProductService;
import lombok.AllArgsConstructor;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {

    private final FileServiceProvider fileServiceProvider;

    @Override
    public List<ProductVO> getList() {
        List<Product> products = this.list(new LambdaQueryWrapper<Product>().orderByAsc(Product::getPriority));
        List<String> fileIds = products.stream().map(Product::getFileId).toList();
        Map<String, String> collect = fileServiceProvider.getFile(fileIds).stream().collect(Collectors.toMap(FileDTO::getFileId, FileDTO::getUrl));
        return products.stream().map(product -> {
            ProductVO productVO = new ProductVO();
            BeanUtils.copyProperties(product, productVO);
            productVO.setId(product.getId());
            productVO.setUrl(collect.get(product.getFileId()));
            return productVO;
        }).toList();
    }
}
